<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">考勤明细</h1>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading"
          @currentRow="currentRow" />
      </el-card>
    </div>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange"
        @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ExaminationInformationInquiry'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import Synchronization from '@/utils/Synchronization'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
const today = new Date() as any
const year = today.getFullYear()
const month = String(today.getMonth() + 1).padStart(2, '0')
const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '部门',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入工号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入姓名',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '入厂日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入年月',
    inputValue: `${year + month}`
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'input',
    inputLabel: '导出考勤报表年月',
    inputPlaceholder: '导出考勤报表年月',
    inputValue: `${year + month}`
  },
  {
    type: 'button',
    checkboxLabel: '导出考勤报表'
  }
]) as any

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出考勤报表') {
    const years = formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '导出考勤报表年月'
    )?.inputValue
    if (!years) {
      JMessage.error('请输入导出考勤报表年月')
      return
    }
    if (tableData.value.length === 0) {
      JMessage.error('请先查询考勤信息')
      return
    }
    const res = (await fileRequest.get(`/emp/ExportAttendanceReport?years=${years}`)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await Synchronization.post<any, any>('/emp/getAll', {
    deptName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '部门')
      ?.selectValue,
    inTime: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入厂日期')
      ?.dateValue?.[0],
    outTime: formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入厂日期')
      ?.dateValue?.[1],
    workerId: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入工号'
    )?.inputValue,
    workerName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入姓名'
    )?.inputValue,
    years: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入年月'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    // 改造成 day1: { value: '', bgColor: 'white' }
    for (const key in item) {
      if (key.includes('day')) {
        const change = item.list.some((item2: any) => Number(key.split('day')[1]) === item2)
        console.log(change)

        if (!change) {
          item[key] = { value: item[key], bgColor: 'white' }
        } else {
          item[key] = { value: item[key], bgColor: '#E0FF00' }
        }
      }
    }
    return item
  })
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const tableHeader = ref([
  {
    label: '月份',
    prop: 'years'
  },
  {
    label: '部门',
    prop: 'deptName'
  },
  {
    label: '工号',
    prop: 'workerId'
  },
  {
    label: '姓名',
    prop: 'workerName'
  },
  {
    label: '入厂日期',
    prop: 'dateIn'
  },
  {
    Multistage: true,
    label: '正班出勤时数(黄色类表示周末)',
    children: [
      {
        bgColor: true,
        width: 50,
        label: '1',
        prop: 'day1'
      },
      {
        bgColor: true,
        width: 50,
        label: '2',
        prop: 'day2'
      },
      {
        bgColor: true,
        width: 50,
        label: '3',
        prop: 'day3'
      },
      {
        bgColor: true,
        width: 50,
        label: '4',
        prop: 'day4'
      },
      {
        bgColor: true,
        width: 50,
        label: '5',
        prop: 'day5'
      },
      { bgColor: true, width: 50, label: '6', prop: 'day6' },
      { bgColor: true, width: 50, label: '7', prop: 'day7' },
      { bgColor: true, width: 50, label: '8', prop: 'day8' },
      { bgColor: true, width: 50, label: '9', prop: 'day9' },
      { bgColor: true, width: 50, label: '10', prop: 'day10' },
      { bgColor: true, width: 50, label: '11', prop: 'day11' },
      { bgColor: true, width: 50, label: '12', prop: 'day12' },
      { bgColor: true, width: 50, label: '13', prop: 'day13' },
      { bgColor: true, width: 50, label: '14', prop: 'day14' },
      { bgColor: true, width: 50, label: '15', prop: 'day15' },
      { bgColor: true, width: 50, label: '16', prop: 'day16' },
      { bgColor: true, width: 50, label: '17', prop: 'day17' },
      { bgColor: true, width: 50, label: '18', prop: 'day18' },
      { bgColor: true, width: 50, label: '19', prop: 'day19' },
      { bgColor: true, width: 50, label: '20', prop: 'day20' },
      { bgColor: true, width: 50, label: '21', prop: 'day21' },
      { bgColor: true, width: 50, label: '22', prop: 'day22' },
      { bgColor: true, width: 50, label: '23', prop: 'day23' },
      { bgColor: true, width: 50, label: '24', prop: 'day24' },
      { bgColor: true, width: 50, label: '25', prop: 'day25' },
      { bgColor: true, width: 50, label: '26', prop: 'day26' },
      { bgColor: true, width: 50, label: '27', prop: 'day27' },
      { bgColor: true, width: 50, label: '28', prop: 'day28' },
      { bgColor: true, width: 50, label: '29', prop: 'day29' },
      { bgColor: true, width: 50, label: '30', prop: 'day30' },
      { bgColor: true, width: 50, label: '31', prop: 'day31' }
    ]
  },
  {
    label: '迟到次数',
    prop: 'lateNums'
  },
  {
    label: '迟到分钟',
    prop: 'lateMinutes'
  },
  {
    label: '早退次数',
    prop: 'earlyNums'
  },
  {
    label: '早退分钟',
    prop: 'earlyMinutes'
  },
  {
    label: '请假分钟',
    prop: 'leaveDays'
  },
  {
    label: '旷工分钟',
    prop: 'outDays'
  },
  {
    label: '正班时数',
    prop: 'workHours'
  }
])
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}

const getSelectList = async () => {
  const mtlDept = formList.value.find(
    (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '部门'
  ) as any
  const mtlDeps = await Synchronization.get<any, any>('/back/deptQuery')
  mtlDeps.forEach((Vendor: any) => {
    mtlDept.selectOptions.push({
      optionLabel: Vendor.deptName,
      optionValue: `${Vendor.deptName}`,
      optionKey: `${Vendor.deptNo}`
    })
  })
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped></style>
